<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="所属学院" size="mini">
      <el-tree
        :data="collegeList"
        :props="collegeListTreeProps"
        node-key="collegeId"
        ref="collegeListTree"
        :default-expand-all="true"
        show-checkbox>
      </el-tree>
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="实验室名称"></el-input>
    </el-form-item>
    <el-form-item label="管理员" size="mini">
      <el-tree
        :data="adminList"
        :props="adminListTreeProps"
        node-key="managerId"
        ref="adminListTree"
        :default-expand-all="true"
        show-checkbox>
      </el-tree>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        collegeList: [],
        adminList: [],
        collegeListTreeProps: {
          label: 'name',
          children: 'children'
        },
        adminListTreeProps: {
          label: 'username',
          children: 'children'
        },
        dataForm: {
          laboratoryId: 0,
          name: '',
          collegeId: 0,
          managerId: 0
        },
        dataRule: {
          collegeId: [
            { required: true, message: '所属学院不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '实验室名称不能为空', trigger: 'blur' }
          ],
          managerId: [
            { required: true, message: '实验室管理员不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/sys/college/list'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.collegeList = treeDataTranslate(data, 'collegeId')
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.$refs.collegeListTree.setCheckedKeys([])
          })
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/sys/user/labAdmin`),
            method: 'post',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0){
              this.adminList = data.adminList
            }
          })
        }).then(() =>{
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/laboratory/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.collegeId = data.Laboratory.collegeId
                this.dataForm.name = data.Laboratory.name
                this.dataForm.managerId = data.Laboratory.managerId
                this.dataForm.createTime = data.Laboratory.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/laboratory/${!this.dataForm.laboratoryId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'laboratoryId': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'collegeId': this.collegeList.collegeId,
                'managerId': this.dataForm.managerName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
